const locale = require('./locale/menu-zh-CN');
const { app, BrowserWindow, Menu } = require('electron');
const isMac = process.platform === 'darwin';
const isDev = process.argv.indexOf('dev') > -1;

function createWindow() {
    const win = new BrowserWindow({
        width: 1024,
        height: 768,
    })
    win.loadFile('./dist/index.html');
};

function createMenu() {
    const template = [{
            label: locale.file.label,
            submenu: [{
                label: locale.file.exit,
                role: isMac ? 'close' : 'quit'
            }]
        },
        {
            label: locale.edit.label,
            submenu: [{
                    label: locale.edit.undo,
                    role: 'undo'
                },
                {
                    label: locale.edit.redo,
                    role: 'redo'
                },
                { type: 'separator' },
                {
                    label: locale.edit.cut,
                    role: 'cut'
                },
                {
                    label: locale.edit.copy,
                    role: 'copy'
                },
                {
                    label: locale.edit.paste,
                    role: 'paste'
                }
            ]
        },
        {
            label: locale.view.label,
            submenu: [{
                    label: locale.view.reload,
                    role: 'reload'
                },
                {
                    label: locale.view.forceReload,
                    role: 'forceReload'
                },
                { type: 'separator' },
                {
                    label: locale.view.actual,
                    role: 'resetZoom'
                },
                {
                    label: locale.view.zoomIn,
                    role: 'zoomIn'
                },
                {
                    label: locale.view.zoomOut,
                    role: 'zoomOut'
                },
                { type: 'separator' },
                {
                    label: locale.view.fullScreen,
                    role: 'togglefullscreen'
                }
            ]
        },
        {
            label: locale.window.label,
            submenu: [{
                    label: locale.window.minimize,
                    role: 'minimize'
                },
                {
                    label: locale.window.zoom,
                    role: 'zoom'
                }
            ]
        }
    ];
    if (isMac) {
        template.unshift({
            label: locale.system.name,
            submenu: [{
                    label: locale.system.about,
                    role: 'about'
                },
                { type: 'separator' },
                { 
                    label: locale.system.services,
                    role: 'services' 
                },
                { type: 'separator' },
                { 
                    label: locale.system.hide,
                    role: 'hide' 
                },
                { 
                    label: locale.system.hideothers,
                    role: 'hideothers' 
                },
                { 
                    label: locale.system.unhide,
                    role: 'unhide' 
                },
                { type: 'separator' },
                { 
                    label: locale.system.quit,
                    role: 'quit' 
                }
            ]
        });
        template[2].submenu.push({ 
                label: locale.edit.pasteAndMatchStyle,
                role: 'pasteAndMatchStyle' 
            },
            {
                label: locale.edit.del,
                role: 'delete'
            },
            {
                label: locale.edit.select,
                role: 'selectAll'
            },
            { type: 'separator' },
            {
              label: locale.edit.speech.label,
              submenu: [
                { 
                    label: locale.edit.speech.startSpeaking,
                    role: 'startSpeaking' 
                },
                { 
                    label: locale.edit.speech.stopSpeaking,
                    role: 'stopSpeaking' 
                }
              ]
            }
        );
        template[4].submenu.push(
            { type: 'separator' },
            { 
                label: locale.window.front,
                role: 'front' 
            },
            { type: 'separator' },
            { 
                label: locale.window.window,
                role: 'window' 
            }
        );
    } else {
        template[1].submenu.push({
            label: locale.edit.del,
            role: 'delete'
        }, 
        { type: 'separator' }, 
        {
            label: locale.edit.select,
            role: 'selectAll'
        });
        template[3].submenu.push({
            label: locale.window.close,
            role: 'close'
        });
    }
    if(isDev) {
        template[isMac ? 3 : 2].submenu.splice(2, 0,
            {
                label: locale.view.devTools,
                role: 'toggleDevTools'
            }
        );
        template.push({
            label: locale.help.label,
            role: "help",
            submenu: [{
                label: locale.help.learn,
                click: async () => {
                    const { shell } = require('electron')
                    await shell.openExternal('https://electronjs.org')
                }
            }]
        })
    }
    return Menu.buildFromTemplate(template);
}

Menu.setApplicationMenu(createMenu());

app.whenReady().then(() => {

    createWindow();

    app.on('activate', function() {
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow();
        }
    });
});

app.on('window-all-closed', function() {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});